package TSP;

public class City 
{
	public int originalId;
	public float posX;
	public float posY;
	public City[] fullNeighbourg;
	private City nextDestination;
	public double[] distance;
	private boolean isUsed = false;
	public City prev;
	
	public City(int id, float x, float y, int totalCity)
	{
		originalId = id;
		posX = x;
		posY = y;
		
		fullNeighbourg = new City[totalCity];
		distance = new double[totalCity];
	}
	
	public void SetNeighbour(int id, City neighbour)
	{
		fullNeighbourg[id] = neighbour;
	}

	public City getNextDestination() {
		return nextDestination;
	}

	public void setNextDestination(City nextDestination) 
	{
		nextDestination.prev = this;
		this.nextDestination = nextDestination;
	}
	public City FindNearestFreeNeighbour()
	{
		City city = null; //GetFirstNonNullNeighbour();
		double bestDistanceTemp = Double.MAX_VALUE;
		double bestDistanceYet = Double.MAX_VALUE;
		for(int i = 0; i < fullNeighbourg.length; i++)
		{
			boolean isMySelf = i == originalId;
			if (!isMySelf)
			{
					
				boolean isAlreadyVisited = fullNeighbourg[i].isUsed();
				double distanceFromThisOneToMe = fullNeighbourg[i].distance[originalId];
				
				
				if (!isAlreadyVisited && distanceFromThisOneToMe < bestDistanceYet)
				{
					city = fullNeighbourg[i];
					bestDistanceYet = distanceFromThisOneToMe;
				}
			}
		}
		return city;
	}
	
	private City GetFirstNonNullNeighbour()
	{
		int i = 0;
		while (fullNeighbourg[i] == null)
		{
			i++;
		}
		
		return fullNeighbourg[i];
	}

	public boolean isUsed() {
		return isUsed;
	}

	public void setUsed(boolean isUsed) {
		this.isUsed = isUsed;
	}
}
